package ft.esc.impl;

import java.io.IOException;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;

/**
 * Send Files in a New Thread.<br />
 * The UI Class
 */
public class EchoSocketClientImpl extends Application
{
	private Stage primaryStage = null;
	
	private static final String FXML_PATH = "tsc.fxml";
	private static final String STAGE_TITLE = "TCP Socket File Send Client";
	
	@Override
	public void start(Stage primaryStage)
	{
		this.primaryStage = primaryStage;
		initFxml();
	}

	private void initFxml()
	{
		Parent form = null;
		
		try
		{
			// 加载 .fxml 布局文件
			FXMLLoader loader = null;
			loader = new FXMLLoader(getClass().getResource(FXML_PATH));
			form = loader.load();
		}
		catch(IOException ex)
		{
			System.out.println(ex.getMessage());
		}
		catch(Exception ex)
		{
			System.out.println(ex.getMessage());
		}
		
		if(form!=null)
		{
			Scene scene = new Scene(form);
			
			// 对stage设置按 ESC 键退出
			scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
				public void handle(KeyEvent arg0)
				{
					if( arg0.getCode() == KeyCode.ESCAPE )
					{
						Platform.exit();
					}
				}
			});
			
			primaryStage.setScene(scene);
			primaryStage.setTitle(STAGE_TITLE);
			
			primaryStage.show();
		}
	}
}
